package com.booter.ws.config;

import com.booter.ws.session.CustomSubProtocolWebSocketHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.support.AbstractSubscribableChannel;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.DelegatingWebSocketMessageBrokerConfiguration;


@Slf4j
@Configuration
public class CustomWebSocketMessageBrokerConfiguration extends DelegatingWebSocketMessageBrokerConfiguration {


    @Autowired
    @Qualifier("socketHandlerScheduler")
    private ThreadPoolTaskScheduler scheduler;

    @Bean
    public CustomSubProtocolWebSocketHandler customSubProtocolWebSocketHandler() {
        return new CustomSubProtocolWebSocketHandler(clientInboundChannel(scheduler), clientOutboundChannel(scheduler));
    }

    //spring boot 低版本使用如下配置
    //    @Override
//    public WebSocketHandler subProtocolWebSocketHandler() {
//        return customSubProtocolWebSocketHandler();
//    }

    //适用与spring boot 2.5版本
    @Override
    @Bean
    public WebSocketHandler subProtocolWebSocketHandler(AbstractSubscribableChannel clientInboundChannel, AbstractSubscribableChannel clientOutboundChannel) {
        return customSubProtocolWebSocketHandler();
    }

}